package com.dsp.web;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.dsp.bean.Calc;
import com.dsp.bean.InsureResult;
import com.dsp.bean.Item;
import com.dsp.services.InsureResultService;

@Controller("InsureResultsController")
public class InsureResultsController extends DspController<InsureResult>{
	
	public static String[] QueryColumn = {"yearMonth","empName","hukou","company"};
	public static String[] QueryType   = {"String","String","Long","Long"};
	
	public InsureResultsController(){
		super(InsureResult.class);
	}

	@Autowired
	private InsureResultService insureResultService = null;
	
	@RequestMapping(value = "/insure_results" , method = RequestMethod.GET)
	public ModelAndView list(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("insure_result/list");
		mav.addObject("path", "insure_result/list.jsp");
		mav.addObject("current", "insure_results");
		return mav;
	}
	
	@RequestMapping(value = "/insure_results_list")
	@ResponseBody
	public Pagination<InsureResult> index(HttpServletRequest request, HttpServletResponse response) throws Exception{
		super.initHttp(request, response);
		DetachedCriteria dc = DetachedCriteria.forClass(InsureResult.class);
		super.getPageParameters(dc, QueryColumn, QueryType);
		Pagination<InsureResult> p = this.insureResultService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}
	
	@RequestMapping(value = "/insure_results_query" , method = RequestMethod.POST)
	@ResponseBody
	public Pagination<InsureResult> query(HttpServletRequest request , HttpServletResponse response , @RequestBody Calc calc){
		int page = Integer.valueOf(request.getParameter("page"));
		int rp = Integer.valueOf(request.getParameter("rp"));

		DetachedCriteria dc = DetachedCriteria.forClass(InsureResult.class);
		if (!DspUtil.isEmpty(calc.getCalcYearMonth())) {
			dc.add(Restrictions.eq("yearMonth", calc.getCalcYearMonth()));
		}
		if (!DspUtil.isEmpty(calc.getEmpName())){
			dc.add(Restrictions.eq("empName", calc.getEmpName()));
		}
		if (!DspUtil.isEmptyList(calc.getCompany())){
			dc.add(Restrictions.in("company", calc.getCompany()));
		}
		if (!DspUtil.isEmptyList(calc.getHukou())){
			dc.add(Restrictions.in("hukou", calc.getHukou()));
		}
		Pagination<InsureResult> p = this.insureResultService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}
	/**
	 * 模糊动态查询
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/insure_results_s" , method = RequestMethod.POST)
	@ResponseBody
	public Pagination<InsureResult> q(HttpServletRequest request , HttpServletResponse response){
		int page = Integer.valueOf(request.getParameter("page"));
		int rp   = Integer.valueOf(request.getParameter("rp"));
		DetachedCriteria dc = DetachedCriteria.forClass(InsureResult.class);
		String qV = request.getParameter("s_yearMonth");
		if (!DspUtil.isEmpty(qV)){
			 super.addDetachedCriteria(dc, "yearMonth", qV ,String.class.toString());
		}
		qV = request.getParameter("s_empName");
		if (!DspUtil.isEmpty(qV)){
			super.addDetachedCriteria(dc, "empName", qV ,String.class.toString());
		}
		qV = request.getParameter("s_company");
		if (!DspUtil.isEmpty(qV)){
			super.addDetachedCriteria(dc, "company", qV, Long.class.toString());
		}
		qV = request.getParameter("s_hukou");
		if (!DspUtil.isEmpty(qV)){
			super.addDetachedCriteria(dc, "hukou", qV, Long.class.toString());
		}
		Pagination<InsureResult> p = this.insureResultService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}
	
	@RequestMapping(value = "/insure_results_download" , method = RequestMethod.POST)
	@ResponseBody
	public String downloadData(HttpServletRequest request , HttpServletResponse response) throws Exception{
		super.initHttp(request, response);
		DetachedCriteria dc = DetachedCriteria.forClass(InsureResult.class);
		super.getPageParameters(dc, QueryColumn, QueryType);
		String path = this.insureResultService.getDownloadData(dc,  DspUtil.getRealPath());
		return "{ \"path\" : \"" +path + "\", \"link\":\""+request.getContextPath() + File.separator + "downloads" + File.separator + path + "\"}";
	}
	
	@RequestMapping(value = "/insure_results_autocomplete" , method = RequestMethod.POST)
	@ResponseBody
	public List<Item> autoComplete(HttpServletRequest request , HttpServletResponse response){
		List<Item> list = new ArrayList<Item>();
		Item it = null;
		it = new Item();
		it.setId(1L);
		it.setName("ABACADEFSDAXSWRTSDQWEQFDSFGFGKLA");
		list.add(it);
		return list;
	}

	/**
	 * @return the insureResultService
	 */
	public InsureResultService getInsureResultService() {
		return insureResultService;
	}

	/**
	 * @param insureResultService the insureResultService to set
	 */
	public void setInsureResultService(InsureResultService insureResultService) {
		this.insureResultService = insureResultService;
	}
}
